require("apm_core")
require("extension")

local G_DEBUG = false

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local function ip2long(ip)
    local ips = string.split(ip, ".")
    local num = 0
    for i,v in pairs(ips) do
        num = num + (tonumber(v) * math.pow(256,#ips-i))
    end
    return num
end

--- main
function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getL7Proto() ~= 80 then return end
    if ApmData.getMsgType() ~= 1 then return end

    local s_title = ApmData.getField("transaction.title", 1)
    if s_title == nil then return end
    ApmData.replaceField("transaction.type_code", s_title)
    ApmData.replaceField("transaction.type_name", s_title)

    local i_src_ip = ip2long(ApmData.getSrcIp())
    local s_channel
    if (i_src_ip >= 173040887 and i_src_ip <= 173040889) then
        s_channel = "VPN用户"
    elseif (i_src_ip >= 173015297 and i_src_ip <= 173018877) then
        s_channel = "内网用户"
    else
        s_channel = "其他用户"
    end

    if s_channel ~= nil then
        ApmData.replaceField("transaction.channel_code", s_channel)
        ApmData.replaceField("transaction.channel_name", s_channel)
    end

    return;
end
